// 运行时配置

// 全局初始化数据配置，用于 Layout 用户信息和权限初始化
// 更多信息见文档：https://umijs.org/docs/api/runtime-config#getinitialstate

import { history, RequestConfig } from '@umijs/max';

import { getUserInfo } from '@/services/UserController';

export async function getInitialState(): Promise<{
    currentUser?: API.UserInfo;
    loading?: boolean;
    fetchUserInfo?: () => Promise<API.UserInfo | undefined>;
}> {
    const fetchUserInfo = async () => {
        try {
            return await getUserInfo();
        } catch (error) {
            history.push('/jump');
        }
        return undefined;
    };
    // 如果不是登录页面，执行
    const { location } = history;
    if (location.pathname !== '/jump') {
        const currentUser = await fetchUserInfo();
        return {
            fetchUserInfo,
            currentUser,
        };
    }
    return {
        fetchUserInfo,
    };
}

export const request: RequestConfig = {
    timeout: 1000,
    // other axios options you want
    errorConfig: {
        errorHandler() {},
        errorThrower() {},
    },
    requestInterceptors: [(url, options) =>
    {

        return { url: `http://localhost:8080${url}`, options }
    },],
    responseInterceptors: [],
};
